package org.dromara.system.service.impl;

import org.dromara.common.core.utils.MapstructUtils;
import org.dromara.common.core.utils.StringUtils;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.common.mybatis.core.page.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.dromara.system.domain.bo.TQuestionBo;
import org.dromara.system.domain.vo.TQuestionVo;
import org.dromara.system.domain.TQuestion;
import org.dromara.system.mapper.TQuestionMapper;
import org.dromara.system.service.ITQuestionService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 问题项Service业务层处理
 *
 * @author Lion Li
 * @date 2024-10-27
 */
@RequiredArgsConstructor
@Service
public class TQuestionServiceImpl implements ITQuestionService {

    private final TQuestionMapper baseMapper;

    /**
     * 查询问题项
     *
     * @param questionId 主键
     * @return 问题项
     */
    @Override
    public TQuestionVo queryById(Integer questionId){
        return baseMapper.selectVoById(questionId);
    }

    /**
     * 分页查询问题项列表
     *
     * @param bo        查询条件
     * @param pageQuery 分页参数
     * @return 问题项分页列表
     */
    @Override
    public TableDataInfo<TQuestionVo> queryPageList(TQuestionBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<TQuestion> lqw = buildQueryWrapper(bo);
        Page<TQuestionVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询符合条件的问题项列表
     *
     * @param bo 查询条件
     * @return 问题项列表
     */
    @Override
    public List<TQuestionVo> queryList(TQuestionBo bo) {
        LambdaQueryWrapper<TQuestion> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<TQuestion> buildQueryWrapper(TQuestionBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<TQuestion> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getSurveyId() != null, TQuestion::getSurveyId, bo.getSurveyId());
        lqw.eq(StringUtils.isNotBlank(bo.getQuestionText()), TQuestion::getQuestionText, bo.getQuestionText());
        lqw.eq(StringUtils.isNotBlank(bo.getQuestionType()), TQuestion::getQuestionType, bo.getQuestionType());
        lqw.eq(bo.getRequired() != null, TQuestion::getRequired, bo.getRequired());
        lqw.eq(bo.getOrderIndex() != null, TQuestion::getOrderIndex, bo.getOrderIndex());
        return lqw;
    }

    /**
     * 新增问题项
     *
     * @param bo 问题项
     * @return 是否新增成功
     */
    @Override
    public Boolean insertByBo(TQuestionBo bo) {
        TQuestion add = MapstructUtils.convert(bo, TQuestion.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setQuestionId(add.getQuestionId());
        }
        return flag;
    }

    /**
     * 修改问题项
     *
     * @param bo 问题项
     * @return 是否修改成功
     */
    @Override
    public Boolean updateByBo(TQuestionBo bo) {
        TQuestion update = MapstructUtils.convert(bo, TQuestion.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(TQuestion entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 校验并批量删除问题项信息
     *
     * @param ids     待删除的主键集合
     * @param isValid 是否进行有效性校验
     * @return 是否删除成功
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Integer> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteByIds(ids) > 0;
    }
}
